package com.pc.controller;

import com.pc.enrty.Order;
import com.pc.enrty.Pay;
import com.pc.enrty.ShoppingAddress;
import com.pc.enrty.ShoppingTrolley;
import com.pc.pojo.OrderSet;
import com.pc.service.OrderService;
import com.pc.service.PayService;
import com.pc.service.ShoppingAddressService;
import com.pc.service.ShoppingTrolleyService;
import com.pc.util.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/order/")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ResultObject result;
    @Autowired
    private PayService payService;
    @Autowired
    private ShoppingAddressService shoppingAddressService;
    @Autowired
    private ShoppingTrolleyService shoppingTrolleyService;

    @RequestMapping("addOrder")
    public boolean addOrder(OrderSet orderSet){
        UUID uuid = UUID.randomUUID();
        String ordernumber = uuid.toString().substring(0,10);
        int pid =  payService.findPay(new Pay(null,orderSet.getPayment())).get(0).getId();
        ShoppingAddress shoppingAddress = new ShoppingAddress();
        shoppingAddress.setDetailedAddress(orderSet.getDetailedAddress());
        int sid= shoppingAddressService.findId(shoppingAddress).get(0).getId();
        Order order = new Order(null,ordernumber,sid,pid,orderSet.getAmount(),null,orderSet.getStatus(),orderSet.getUid());
        boolean b = orderService.addOrder(order);
        return b;
    }

    @RequestMapping("removeShoppingTrolley")
    public boolean removeShoppingTrolley(ShoppingTrolley shoppingTrolley){
        boolean b = shoppingTrolleyService.removeByUidAndFid(shoppingTrolley);
        return b;
    }
}
